﻿new Module('CobraCms.Js', function () {
    var Class = Phosphor.Utilities.Class;

    var ContentTreeNode = new Class({
        JsTreeNode: {},

        initialize: function (options) {
            this.JsTreeNode = options;
        },

        AppendChildNode: function (index, childNode) { 
            var childJsTreeNode = CobraCms.Js.ContentTreeUtilities.BuildJsTreeNode(childNode);
            this.JsTreeNode.addChild(childJsTreeNode);     
        },

        ReloadSuccess: function (data) {
            $.each(data, this.AppendChildNode.bind(this));
            this.JsTreeNode.setLazyNodeStatus(DTNodeStatus_Ok);
        },

        ReloadError: function (jqHR, textStatus, errorThrown) {
            this.JsTreeNode.setLazyNodeStatus(DTNodeStatus_Error, {
                tooltip: textStatus,
                info: textStatus
            });
        },

        Reload: function () {
            var url = '/ajax/content/tree/nodes/' + this.JsTreeNode.data.contentNode.NodeId + '/children';
            $.ajax({
                url: url,
                success: this.ReloadSuccess.bind(this),
                error: this.ReloadError.bind(this)
            });
        }
    });

    this.ContentTreeNode = ContentTreeNode;
});